package com.ruoyi.hatha.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hatha.domain.PYuyue;
import com.ruoyi.hatha.service.IPKechengService;
import com.ruoyi.hatha.service.IPYuyueService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hatha.domain.PMykc;
import com.ruoyi.hatha.service.IPMykcService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的课程Controller
 * 
 * @author Dong
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/hatha/mykc")
public class PMykcController extends BaseController
{
    @Autowired
    private IPMykcService pMykcService;
    @Autowired
    private IPKechengService kechengService;

    @Autowired
    private IPYuyueService yuyueService;

    /**
     * 查询我的课程列表
     */
    @GetMapping("/list")
    public AjaxResult list(PMykc pMykc)
    {
        pMykc.setUid(SecurityUtils.getUserId());
        pMykc.setIsused(0);
        List<PMykc> list = pMykcService.selectPMykcList(pMykc);

        for(int i=0;i<list.size();i++){
            Long kic = list.get(i).getKcid();
            list.get(i).setKecheng(kechengService.selectPKechengById(kic));

            PYuyue pyy = new PYuyue();
            pyy.setUid(SecurityUtils.getUserId());
            pyy.setMkid(list.get(i).getId());
            List<PYuyue> lyy = yuyueService.selectPYuyueList(pyy);

            if(lyy.size()>0){
                list.get(i).setIsyyed(1);
            }else{
                list.get(i).setIsyyed(0);
            }

        }

        return AjaxResult.success(list);
    }

    /**
     * 导出我的课程列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:mykc:export')")
    @Log(title = "我的课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PMykc pMykc)
    {
        List<PMykc> list = pMykcService.selectPMykcList(pMykc);
        ExcelUtil<PMykc> util = new ExcelUtil<PMykc>(PMykc.class);
        util.exportExcel(response, list, "我的课程数据");
    }

    /**
     * 获取我的课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('hatha:mykc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pMykcService.selectPMykcById(id));
    }

    /**
     * 新增我的课程
     */
    @PreAuthorize("@ss.hasPermi('hatha:mykc:add')")
    @Log(title = "我的课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PMykc pMykc)
    {
        return toAjax(pMykcService.insertPMykc(pMykc));
    }

    /**
     * 修改我的课程
     */
    @PreAuthorize("@ss.hasPermi('hatha:mykc:edit')")
    @Log(title = "我的课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PMykc pMykc)
    {
        return toAjax(pMykcService.updatePMykc(pMykc));
    }

    /**
     * 删除我的课程
     */
    @PreAuthorize("@ss.hasPermi('hatha:mykc:remove')")
    @Log(title = "我的课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pMykcService.deletePMykcByIds(ids));
    }
}
